/*
* License:  This  program  is  free  software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the  Free Software Foundation; either version 3 of the License, or (at your
* option)  any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*/

package pcgen.CharacterViewer.resources;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

import pcgen.android.Logger;

public class GameResourceModifier  extends GameResourceParcelable
{
	public GameResourceModifier()
	{	
	}
	
	public GameResourceModifier(Parcel in)
	{
		super(in);
	}
	
	public static final Parcelable.Creator<GameResourceModifier> CREATOR = new Parcelable.Creator<GameResourceModifier>() 
	{
		public GameResourceModifier createFromParcel(Parcel in) 
		{
		    return new GameResourceModifier(in);
		}
		
		public GameResourceModifier[] newArray(int size) 
		{
		    return new GameResourceModifier[size];
		}
	};
	
	public int getModifier()
	{
		return _modifier;
	}
	
	public ModifierTypes getModifierType()
	{
		return _modifierType;
	}
	
	public String getText()
	{
		return _text;
	}
	
	public int getValue()
	{
		return _value;
	}
	
	public boolean isDamage()
	{
		return isType(ModifierTypes.Damage);
	}
	
	public boolean isHealing()
	{
		return isType(ModifierTypes.Healing);
	}
	
	public boolean isTemp()
	{
		return isType(ModifierTypes.Temp);
	}
	
	public boolean isType(ModifierTypes type)
	{
		return (_modifierType == type);
	}
	
	public JSONObject load(JSONObject data)
		throws JSONException
	{
		setModifier(getJSONInt(data, "modifier"));
		setValue(getJSONInt(data, "value"));
		setText(getJSONString(data, "text"));
		setModifierType(ModifierTypes.valueOf(getJSONString(data, "modifierType")));
	    
	    return data;
	}
	
	public JSONObject save() 
		throws JSONException
	{
	    JSONObject data = new JSONObject();
	    
	    data.put("modifier", getModifier());
	    data.put("value", getValue());
	    data.put("text", _text);
	    data.put("modifierType", _modifierType.toString());
	    
	    return data;
	}
	
	public void setModifier(int value)
	{
		_modifier = value;
	}
	
	public void setModifierType(ModifierTypes type)
	{
		_modifierType = type;
	}
	
	public void setValue(int value)
	{
		_value = value;
	}
	
	public void setText(String value)
	{
		_text = value;
	}
	
	public void setValue(ModifierTypes type, int value)
	{
		_modifierType = type;
		_value = value;
	}

	@Override
	protected void readFromParcelTransform(Parcel in) 
		throws Throwable
	{
		try
    	{
			setModifier(in.readInt());
			setValue(in.readInt());
			setText(in.readString());
			setModifierType(ModifierTypes.valueOf(in.readString()));
		}
    	catch (Throwable tr)
    	{
    		Logger.e(TAG, "readFromParcelTransform", tr);
    		throw tr;
    	}
	}

	@Override
	protected void writeToParcelTransform(Parcel dest, int flags)
		throws Throwable
	{
		try
    	{
			dest.writeInt(getModifier());
			dest.writeInt(getValue());
			dest.writeString(_text);
			dest.writeString(_modifierType.toString());
		}
    	catch (Throwable tr)
    	{
    		Logger.e(TAG, "writeToParcelTransform", tr);
    		throw tr;
    	}
	}
	
	private int _modifier;
	private String _text;
	private int _value;
	private ModifierTypes _modifierType;
	
	public enum ModifierTypes
	{
		Damage,
		Healing,
		Temp
	}

	private static final String TAG = GameResourceModifier.class.getSimpleName();
}
